package com.fcat.common.user.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fcat.springboot.mybatis.controller.TsuantoneBaseController;
import com.fcat.springboot.mybatis.util.JsonUtil;
import com.fcat.base.common.util.StrUtil;
import com.fcat.common.user.entity.Dict;
import com.fcat.common.user.service.DictService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("dict")
public class DictController extends TsuantoneBaseController<DictService, Dict, Integer> {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "2.1.3.获取字典表", notes = "")
    @GetMapping(value = "/getDictByTypeCode")
    public JSONObject getDictByTypeCode(@RequestParam("typeCode") String typeCode) {
        if (!StrUtil.isBlank(typeCode)) {
            List<Dict> dictList = dictService.getListByTypeCode(typeCode);
            JSONArray jsonArray = new JSONArray();
            for (Dict dict : dictList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("value", dict.getValue());
                jsonObject.put("name", dict.getName());
                jsonObject.put("desc", dict.getDesc());
                jsonObject.put("rank", dict.getRank());
                jsonArray.add(jsonObject);
            }
            return JsonUtil.getSuccessJsonObject(jsonArray);
        }
        return JsonUtil.getSuccessJsonObject();
    }

    @ApiOperation(value = "2.1.3.获取字典表", notes = "")
    @GetMapping(value = "/getDictByTypeCodes")
    public JSONObject getDictByTypeCodes(String typeCodes) {
        List<Dict> dictList=new ArrayList<>();
        if (!StrUtil.isBlank(typeCodes)) {
            List<String> typeCodeList = new ArrayList<>();
            String[] array = typeCodes.split(":");
            if (array.length == 0) {
                return JsonUtil.getSuccessJsonObject();
            }
            for (String s : array) {
                if (!StrUtil.isBlank(s)) {
                    typeCodeList.add(s);
                }
            }
            dictList = dictService.getListByTypeCodes(typeCodeList);
        }else{
            dictList = dictService.getListByTypeCodes(null);
        }

        JSONArray subArray = null;
        JSONArray jsonArray = new JSONArray();
        String preTypeCode = "";
        String preDesc="";

        if (!CollectionUtils.isEmpty(dictList)) {
            for (Dict dict : dictList) {
                if (!preTypeCode.equals(dict.getTypeCode())) {
                    if (!preTypeCode.equals("")) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("typeCode", preTypeCode);
                        jsonObject.put("desc", preDesc);
                        jsonObject.put("list", subArray);
                        jsonArray.add(jsonObject);
                    }
                    subArray = new JSONArray();
                }
                JSONObject obj = new JSONObject();
                obj.put("value", dict.getValue());
                obj.put("name", dict.getName());
                obj.put("rank", dict.getRank());
                if (subArray != null) {
                    subArray.add(obj);
                }
                preTypeCode = dict.getTypeCode();
                preDesc=dict.getDesc();
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("typeCode", preTypeCode);
            jsonObject.put("desc", preDesc);
            jsonObject.put("list", subArray);
            jsonArray.add(jsonObject);
        }
        return JsonUtil.getSuccessJsonObject(jsonArray);
    }



    @ApiOperation(value="字典表测试",notes="")
    @RequestMapping(value="/getNameByValue",method= RequestMethod.GET)
    public JSONObject getNameByValue(@RequestParam("value") String value) {
        if (StrUtil.isBlank(value)) {
            return JsonUtil.getFailJsonObject("参数为空");
        }
        String name=dictService.getNameByValue(value);
        return JsonUtil.getSuccessJsonObject(name);
    }

    @ApiOperation(value = "测试", notes = "")
    @RequestMapping(value = "/project", method = RequestMethod.GET)
    public JSONObject project(@RequestParam("typeCode") String typeCode) {
        if (StrUtil.isBlank(typeCode)) {
            return JsonUtil.getFailJsonObject("参数为空");
        }
        List<Dict> dictList = dictService.getListByTypeCode(typeCode);
        return JsonUtil.getSuccessJsonObject(dictList);
    }


}